<?php

/**
* 股票信息表
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Stockcode  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	* 股票信息表列表
	*/
	public function index($stop = 3, $bull_sell = 0, $bull_sell_type = 0, $code = 0 ,$name = 0 , $p = 1){
		//通用搜索
		$where = " where open = 1";
		
		//停牌搜索1:停牌，0：无停
		$stop = intval($stop);
		if($stop != 3){
			$where .= " AND stop_pai = '$stop'";
		}
		
		//买卖搜索1：可买，2：可卖，3：禁买，4：禁卖
		$bull_sell = intval($bull_sell);
		if($bull_sell == 1){
			$where .= " AND can_bull = 1";
		} elseif($bull_sell == 2){
			$where .= " AND can_sell = 1";
		} elseif($bull_sell == 3){
			$where .= " AND can_bull = 0";
		} elseif($bull_sell == 4){
			$where .= " AND can_sell = 0";
		}
		
		//升跌搜索1：可买升，2：禁买升，3：可买跌，4：禁买跌
		$bull_sell_type = intval($bull_sell_type);
		if($bull_sell_type == 1){
			$where .= " AND can_bull_up = 1";
		} elseif($bull_sell_type == 2){
			$where .= " AND can_bull_up = 0";
		} elseif($bull_sell_type == 3){
			$where .= " AND can_bull_down = 1";
		} elseif($bull_sell_type == 4){
			$where .= " AND can_bull_down = 0";
		}
		
		//股票代码搜索
		$code = sql_format($code);
		if($code){
			$where .= " AND code like '%$code%'";
		} else{
			$code = 0;
		}
		
		//股票名字搜索
		$name = sql_format($name);
		if($name){
			$where .= " AND name like '%$name%'";
		} else{
			$name = 0;
		}
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//查询总数
		$sql = "select count(*) as c from stock_code" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//分页查询
		$sql = "select *  from stock_code" . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = '/' . $this->_controller . '/' . $this->_action  . '/' . $stop. '/' . $bull_sell . '/'  . $bull_sell_type . '/' . $code. '/' . $name . '/';
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("code", $code);
		$this->assign("name", $name);
		$this->assign("stop", $stop);
		$this->assign("bull_sell", $bull_sell);
		$this->assign("bull_sell_type", $bull_sell_type);
		$this->display();
	}
	
	/**
	* 除权除息
	*/
	public function drxd($stop = 3, $bull_sell = 0, $bull_sell_type = 0, $code = 0 ,$name = 0 , $p = 1){
		//通用搜索
		$where = " where open = 0";
		
		//停牌搜索1:停牌，0：无停
		$stop = intval($stop);
		if($stop != 3){
			$where .= " AND stop_pai = '$stop'";
		}
		
		//买卖搜索1：可买，2：可卖，3：禁买，4：禁卖
		$bull_sell = intval($bull_sell);
		if($bull_sell == 1){
			$where .= " AND can_bull = 1";
		} elseif($bull_sell == 2){
			$where .= " AND can_sell = 1";
		} elseif($bull_sell == 3){
			$where .= " AND can_bull = 0";
		} elseif($bull_sell == 4){
			$where .= " AND can_sell = 0";
		}
		
		//升跌搜索1：可买升，2：禁买升，3：可买跌，4：禁买跌
		$bull_sell_type = intval($bull_sell_type);
		if($bull_sell_type == 1){
			$where .= " AND can_bull_up = 1";
		} elseif($bull_sell_type == 2){
			$where .= " AND can_bull_up = 0";
		} elseif($bull_sell_type == 3){
			$where .= " AND can_bull_down = 1";
		} elseif($bull_sell_type == 4){
			$where .= " AND can_bull_down = 0";
		}
		
		//股票代码搜索
		$code = sql_format($code);
		if($code){
			$where .= " AND code like '%$code%'";
		} else{
			$code = 0;
		}
		
		//股票名字搜索
		$name = sql_format($name);
		if($name){
			$where .= " AND name like '%$name%'";
		} else{
			$name = 0;
		}
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//查询总数
		$sql = "select count(*) as c from stock_code" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//分页查询
		$sql = "select *  from stock_code" . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = '/' . $this->_controller . '/' . $this->_action  . '/' . $stop. '/' . $bull_sell . '/'  . $bull_sell_type . '/' . $code. '/' . $name . '/';
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("code", $code);
		$this->assign("name", $name);
		$this->assign("stop", $stop);
		$this->assign("bull_sell", $bull_sell);
		$this->assign("bull_sell_type", $bull_sell_type);
		$this->display();
	}
	
	/**
	* 新增股票信息表
	* @param    int  	 $id  			id
	* @param    char  	 $sn   			SN号
	* @param    char  	 $code   		股票代码
	* @param    char  	 $name   		股票名称
	* @param    char  	 $pinyin    	拼音缩写
	* @param    char  	 $type   		类型，sz深市，sh沪市
	* @param    tinyint  $open   		开盘:1-开盘，0-关盘
	* @param    char  	 $dy   			所属地域，来自config_stock
	* @param    char  	 $hy   			所属行业，来自config_stock
	* @param    int  	 $min_num  		最小成交额，低于此值不能买入
	* @param    tinyint  $stop_pai  	停牌:1-停牌，0-未停牌
	* @param    int  	 $stop_date 	停牌日期
	* @param    tinyint  $can_bull  	可买:1-可买，0-禁买
	* @param    tinyint  $can_sell  	可卖:1-可卖，0-禁卖
	* @param    tinyint  $can_bull_up   可买升:1-可买升 0-禁买升
	* @param    tinyint  $can_bull_down 可买降: 1-可买降 0-禁买降
	* @param    char  	 $bk_list   	所属板块
	*/
	public function add(){
		if(IS_POST){
			$code = isset($_POST["code"]) ? substr(sql_format($_POST["code"]), 0, 6) : "";
			$name = isset($_POST["name"]) ? substr(sql_format($_POST["name"]), 0, 20) : "";
			$pinyin = isset($_POST["pinyin"]) ? substr(sql_format($_POST["pinyin"]), 0, 10) : "";
			$type = isset($_POST["type"]) ? substr(sql_format($_POST["type"]), 0, 4) : "";
			$open = isset($_POST["open"]) ? substr(sql_format($_POST["open"]), 0, 1) : "";
			$min_num = isset($_POST["min_num"]) ? substr(sql_format($_POST["min_num"]), 0, 10) : "";
			$stop_pai = isset($_POST["stop_pai"]) ? substr(sql_format($_POST["stop_pai"]), 0, 1) : "";
			$stop_date =  isset($_POST["stop_date"]) ? $_POST["stop_date"] : "0";
			$can_bull = isset($_POST["can_bull"]) ? substr(sql_format($_POST["can_bull"]), 0, 1) : "";
			$can_sell = isset($_POST["can_sell"]) ? substr(sql_format($_POST["can_sell"]), 0, 1) : "";
			$can_bull_up = isset($_POST["can_bull_up"]) ? substr(sql_format($_POST["can_bull_up"]), 0, 1) : "";
			$can_bull_down = isset($_POST["can_bull_down"]) ? substr(sql_format($_POST["can_bull_down"]), 0, 1) : "";
			$bk_list = isset($_POST["bklist_hidden"]) ? substr(sql_format($_POST["bklist_hidden"]), 0, 255): "";
			$bk_list = trim($bk_list, ",");
			if(! $code){
				ajax_return(0, "请输入代号");
			}
			if(! $name){
				ajax_return(0, "请输入名称");
			}
			if(! $pinyin){
				ajax_return(0, "请输入拼音");
			}
			
			$sql = "select id from stock_code where code = '$code'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "代码已经存在");
			$sn ="";
			$hy="";
			$dy="";
			
			$sql = "insert into stock_code (sn,code,name,pinyin,type,open,dy,hy,min_num,stop_pai,stop_date,can_bull,can_sell,can_bull_up,can_bull_down,bk_list) values ('$sn','$code','$name','$pinyin','$type','$open','$dy','$hy','$min_num','$stop_pai','" . strtotime($stop_date) ."','$can_bull','$can_sell','$can_bull_up','$can_bull_down','$bk_list')";
			$this->db->query($sql);
			$res =$this->db->insert_id();
			if(! $res){
				ajax_return( 0, "添加失败" );
			} else{
				ajax_return( 1, "添加成功" , "/Stock" );
			}
		}
		$sql = "select class,bk_name from config_stock order by id ASC,class ASC,bk_name ASC";
		$bklist=$this->db->query($sql)->result_array();
		$this->assign("bklist",$bklist);
		$this->display();
	}
	
	/**
	* 编辑股票信息表
	* @param    int  	 $id  			id
	* @param    char  	 $sn   			SN号
	* @param    char  	 $code   		股票代码
	* @param    char  	 $name   		股票名称
	* @param    char  	 $pinyin    	拼音缩写
	* @param    char  	 $type   		类型，sz深市，sh沪市
	* @param    tinyint  $open   		开盘:1-开盘，0-关盘
	* @param    char  	 $dy   			所属地域，来自config_stock
	* @param    char  	 $hy   			所属行业，来自config_stock
	* @param    int  	 $min_num  		最小成交额，低于此值不能买入
	* @param    tinyint  $stop_pai  	停牌:1-停牌，0-未停牌
	* @param    int  	 $stop_date 	停牌日期
	* @param    tinyint  $can_bull  	可买:1-可买，0-禁买
	* @param    tinyint  $can_sell  	可卖:1-可卖，0-禁卖
	* @param    tinyint  $can_bull_up   可买升:1-可买升 0-禁买升
	* @param    tinyint  $can_bull_down 可买降: 1-可买降 0-禁买降
	* @param    char  	 $bk_list   	所属板块
	*/
	public function edit($id = 0){
		if(IS_POST){
			
			if(0 >= $id){
				ajax_return( 0 , "参数有误" );
			}
			
			$code = isset($_POST["code"]) ? substr(sql_format($_POST["code"]), 0, 6) : "";
			$name = isset($_POST["name"]) ? substr(sql_format($_POST["name"]), 0, 20) : "";
			$pinyin = isset($_POST["pinyin"]) ? substr(sql_format($_POST["pinyin"]), 0, 10) : "";
			$type = isset($_POST["type"]) ? substr(sql_format($_POST["type"]), 0, 4) : "";
			$open = isset($_POST["open"]) ? substr(sql_format($_POST["open"]), 0, 1) : "";
			
			$min_num = isset($_POST["min_num"]) ? substr(sql_format($_POST["min_num"]), 0, 10) : "";
			$stop_pai = isset($_POST["stop_pai"]) ? substr(sql_format($_POST["stop_pai"]), 0, 1) : "";
			$stop_date =  isset($_POST["stop_date"]) ? $_POST["stop_date"] : "0";
			$can_bull = isset($_POST["can_bull"]) ? substr(sql_format($_POST["can_bull"]), 0, 1) : "";
			$can_sell = isset($_POST["can_sell"]) ? substr(sql_format($_POST["can_sell"]), 0, 1) : "";
			$can_bull_up = isset($_POST["can_bull_up"]) ? substr(sql_format($_POST["can_bull_up"]), 0, 1) : "";
			$can_bull_down = isset($_POST["can_bull_down"]) ? substr(sql_format($_POST["can_bull_down"]), 0, 1) : "";
			$bk_list = isset($_POST["bklist_hidden"]) ? substr(sql_format($_POST["bklist_hidden"]), 0, 255): "";
			$bk_list = trim($bk_list, ",");
			if(! $code){
				ajax_return(0, "请输入代码");
			}
			if(! $name){
				ajax_return(0, "请输入名称");
			}
			if($stop_date && !strtotime($stop_date)){
				ajax_return(0, "请输入停牌日期");
			}
			$sql = "select id from stock_code where id<>$id   AND code = '$code'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "代号已经存在");
			
			$sql = "update stock_code set code = '$code',name = '$name',pinyin = '$pinyin',type = '$type',open = '$open',min_num = '$min_num',stop_pai = '$stop_pai',stop_date = '" . strtotime($stop_date) . "',can_bull = '$can_bull',can_sell = '$can_sell',can_bull_up = '$can_bull_up',can_bull_down = '$can_bull_down',bk_list='$bk_list' where id = $id";
			
			$this->db->query($sql);
			
			//返回结果集
			ajax_return( 1, "更新成功" , "/Stock" );
		} else{
			if(0 >= $id){
				$this->error("参数有误");
			}
			$sql = "select * from stock_code where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				$this->error("记录不存在");
			}
			$sql = "select class,bk_name from config_stock order by id ASC,class ASC,bk_name ASC";
			$bklist=$this->db->query($sql)->result_array();
			$selected_bk_list=array();
			if($result["bk_list"]){
				$selected_bk_list =  array_filter(explode(',',$result["bk_list"]));
			}
			
			$this->assign("bklist",$bklist);
			$this->assign("selected_bk_list",$selected_bk_list);
			$this->assign("result", $result);
			$this->display();
		}
	}
	
	/**
	* 删除
	* @param	int		$id			股票id
	* @param	array	$result 	返回结果
	*/
	public function delete($id = 0){
		if(IS_POST){
			!$id &&	ajax_return(0, "参数有误");
			$sql = "delete from  stock_code where id in ($id)";
			$this->db->query($sql);
			ajax_return(1, "删除成功");
		}
	}
	
	/**
	* 更改状态
	* @param	int		$id			股票id
	* @param	string	$field		更新字段
	* @param	array	$result 	返回结果
	*/
	public function change($field = '', $id = 0){
		if(IS_POST){
			//校验参数
			if(0 >= $id){
				ajax_return(0, "非法请求");
			}

			//查询记录
			$sql = "select $field from stock_code where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				ajax_return(0, "数据不存在");
			}
			
			//判断当前状态
			if($result[$field] == 0){
				$value = 1;
			}
			else{
				$value = 0;
			}

			//更新记录
			$sql = "update stock_code set $field = '$value' where id = $id";
			$this->db->query($sql);
			$return["field"] = $field;
			$return["value"] = $value;
			$return["id"] = $id;
			
			//返回结果集
			ajax_return(1, "状态已更新", $return);
		}
	}
	
	/**
	* 检查新股
	*/
	public function check() {
		$result = get_new_stock();
		$this->assign("result", $result);
		$this->display();
	}
}
